var ctx;
var userAccount = "";
var cellId;
var shopId;
var pSize;

function setCtx(ctx){
	this.ctx = ctx;
}

function gotoPage(page){
	userAccount = ifNull($("#userAccount").val());
	cellId = ifNull($("#cellid").val());
	shopId = ifNull($("#shopid").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(userAccount == $("#userAccount").attr("placeholder")){
		userAccount = "";
	}
	loadContent("/couponInfo/couponApply?pNo="+ page +"&userAccount="+ userAccount +"&cellId="+ cellId +"&shopId="+ shopId + "&pSize="+ pSize);
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

//条件搜索用户列表
function getCouponInfoList(){
	userAccount = ifNull($("#userAccount").val());
	cellId = ifNull($("#cellid").val());
	shopId = ifNull($("#shopid").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(userAccount == $("#userAccount").attr("placeholder")){
		userAccount = "";
	}
	loadContent("/couponInfo/couponApply?pNo=1&userAccount="+ userAccount +"&cellId="+ cellId +"&shopId="+ shopId + "&pSize="+ pSize);
	
}

function gotoCouponInfoPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	userAccount = ifNull($("#userAccount").val());
	cellId = ifNull($("#cellid").val());
	shopId = ifNull($("#shopid").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(userAccount == $("#userAccount").attr("placeholder")){
		userAccount = "";
	}
	loadContent("/couponInfo/couponApply?pNo="+ page +"&userAccount="+ userAccount +"&cellId="+ cellId +"&shopId="+ shopId + "&pSize="+ pSize);
}


function approved(id, state) {
	var message;
	switch (parseInt(state)) {
	case 1:
		message = "是否拒绝通过？";
		break;
	case 2:
		message = "是否禁用？";
		break;
	case 3:
		message = "是否启用？";
		break;
	case 4:
		message = "是否通过？";
		break;
	case 5:
		message = "是否启用？";
		break;
	default:
		break;
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/couponInfo/couponApplyUpdate",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				state : state
			},
			success : function(result) {
				if (result.success) {
					// alert("操作成功");

					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						if (state == 1) {
							loadContent("/couponInfo/couponApply");
						} else {
							loadContent("/couponInfo/couponApply");
						}

					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}


//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}


function deleteByIds(){
	if($("img[name='couponApplyChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='couponApplyChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    var vals = delIds.join(',');//转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/couponInfo/deleteByPrimaryKey",{
					ids:vals
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							loadContent("/couponInfo/couponApply");
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});

		
	}
}
